$(document).ready(function () {
    // 定义变量
    let usernameError = false;
    let emailError = false;

    // 验证用户名
    $("#username").on("blur", function () {
        let username = $(this).val();
        if (!username || username == "" || username.trim().length < 2) {
            usernameError = true;
            $("#username-error").text("不能为空，或小于1个字符");
        }
        else {
            $.ajax({
                url: "/api/check_username/",
                type: "GET",
                data: {
                    'username': username
                },
                success: function (response) {
                    if (response.status == 1) {
                        usernameError = true;
                        $("#username-error").text("用户名已存在");
                    }
                    else {
                        usernameError = false;
                        $("#username-error").text("");
                    }
                },
                error: function () {
                    alert("服务器错误，请稍后再试");
                }
            });
        }
    });

    // 验证邮箱
    $("#email").on("blur", function () {
        let email = $(this).val();
        let emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
        if (!emailPattern.test(email)) {
            emailError = true;
            $("#email-error").text("邮箱格式不正确");
        }
        else {
            $.ajax({
                url: "/api/check_email/",
                type: "GET",
                data: {
                    'email': email
                },
                success: function (response) {
                    if (response.status == 1) {
                        emailError = true;
                        $("#email-error").text("邮箱已存在");
                    }
                    else {
                        emailError = false;
                        $("#email-error").text("");
                    }
                },
                error: function () {
                    alert("服务器错误，请稍后再试");
                }
            })
        }
    });

    // 验证表单
    // 验证密码 在表单提交submit时，触发函数
    $("#register-form").on("submit", function (event) {
        let password = $("#password").val();
        let confirm_password = $("#confirm_password").val();
        let passwordPattern = /[a-zA-Z]\d|\d[a-zA-Z]/;

        if (!passwordPattern.test(password) || password.length < 6) {
            $("#password-error").text("密码长度不足6位，且必须包含字母和数字");
            // 阻止表单提交
            event.preventDefault();
        }
        else {
            if (password != confirm_password) {
                $("#password-error").text("两次密码不一致");
                // 阻止表单提交
                event.preventDefault();
            }
            else {
                if (usernameError || emailError) {
                    $("#password-error").text("部分信息不符合要求，请重新填写");
                    // 阻止表单提交
                    event.preventDefault();
                }
                else {
                    // 清空错误提示，表单提交
                    $("#password-error").text("");
                }
            }
        }
    });
})